<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class TaskController extends Controller {

    //得到任务list，1：问答，2新闻
    public function taskList(){
    	if(session('name')){
    		$where = array();
    		$taskTable = D('Task');
    		if(I('param.type','')){
    			$where['task_type']=I('param.type','');
    		}
    		$taskTable->where($where);
    		if(!empty(I('param.page','')) && !empty(I('param.pageCount',''))){
    			$taskTable->page(I('param.page'),I('param.pageCount'));
    		}
    		$task = $taskTable->select();
    		$scheduleTable = M('Schedule');
    		$seTable = M('SearchEngine');
    		foreach ($task as &$ta){
    			$ta['schedule']= array();
    			if($ta['schedule_type'] == 2){
    				$schedule = $scheduleTable->field('time')->where(array('task_id'=>$ta['id']))->select();
    				$ta['schedule'] = $schedule;
    			}
    			$ta['se_name']= array();
    			if($ta['search_id']!=null){
    				$idArr = explode(',', $ta['search_id']);
    				foreach ($idArr as $id){
    					 $result = $seTable->field('se_name')->where(array('id'=>$id))->select();
    					 $ta['se_name'][]=$result[0]['se_name'];
    				}
    			}
    		}
    		echo json_encode(array('result'=>$task,'count'=>$taskTable->where($where)->count(),'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //添加
    public function addTask(){
    	if (session ( 'name' )) {
    		$taskTable = D('Task');
    		$data ['page_count'] = I ( 'param.page_count', 1 );
    		$data ['task_name'] = I ( 'param.task_name', '' );
    		$data ['task_type'] = I ( 'param.task_type', 1 );
    		$data ['enabled'] = I ( 'param.enabled', 0 );
    		$data ['url_start'] = I ( 'param.url_start', '' );
    		$data ['rule_match'] = I ( 'param.rule_match', '' );
    		$data ['count_thread_max'] = I ( 'param.count_thread_max', 1 );
    		$data ['time_interval'] = I ( 'param.time_interval', 10 );
    		$data ['schedule_type'] = I ( 'param.schedule_type', 1 );
    		$data ['search_key'] = I ( 'param.search_key', '' );
    		$data ['search_id'] = I ( 'param.search_id', '' );
    		$map['task_name'] = $data ['task_name'];
    		if(empty($data ['task_name'])){
    			die(json_encode ( array ('result' => '任务名称不能为空',	'code' => 0)));
    		}
    		if($data ['task_type'] == 1){
	    		if(empty($data ['url_start'])){
	    			die(json_encode ( array ('result' => '采集来源不能为空',	'code' => 0)));
	    		}
    		}
    		if($data ['count_thread_max']>100){
    			die(json_encode ( array ('result' => '最大线程数不能大于100',	'code' => 0)));
    		}
    		if($data ['task_type'] == 2){
    			if(empty($data ['search_key'])){
    				die(json_encode ( array ('result' => '搜索关键字不能为空',	'code' => 0)));
    			}
    			if(empty($data ['search_id'])){
    				die(json_encode ( array ('result' => '搜索引擎不能为空',	'code' => 0)));
    			}
    		}
    		$task = $taskTable->where ( $map )->select ();
    		if ($task) {
    			echo json_encode ( array (
    					'result' => '名称重复',
    					'code' => 0
    			) );
    			exit ();
    		}
    		$task_id = $taskTable->add ( $data );
    		if($data ['schedule_type'] == 2){
    			$scheduleArr = I('param.schedule_data',array());
    			if($scheduleArr){
    				$scheduleTable = M('Schedule');
    				foreach ($scheduleArr as $schedule){
    					$scheduleData = array();
    					$scheduleData['task_id']= $task_id;
    					$scheduleData['time']= $schedule;
    					$scheduleTable->add($scheduleData);
    				}
    			}
    		}
    		echo json_encode ( array (
    				'result' => '添加成功',
    				'code' => 1
    		) );
    	} else {
    		echo json_encode ( array (
    				'result' => '登录失效重新登陆',
    				'code' => 0
    		) );
    	}
    }
    
    //任务修改
    public function updateTask(){
    	if(session('name')){
    		$taskTable = D('Task');
    		$map['id'] = I('param.id');
    		$data=array();
    		if(!empty(I('param.page_count',''))){
    			$data['page_count'] = I('param.page_count','');
    		}
    		if(!empty(I('param.task_name',''))){
    			$data['task_name'] = I('param.task_name','');
    			$mymap['task_name'] = $data ['task_name'];
    			$mymap['id'] = array('neq',I('param.id'));
    			$task = $taskTable->where ( $mymap )->select ();
    			if ($task) {
    				echo json_encode ( array (
    						'result' => '名称重复',
    						'code' => 0
    				) );
    				exit ();
    			}
    		}
    		if(!empty(I('param.task_type',''))){
    			$data['task_type'] = I('param.task_type','');
    		}
    		if(I('param.enabled')!=null ){
    			$data['enabled'] = I('param.enabled','');
    		}
    		if(!empty(I('param.url_start',''))){
    			$data['url_start'] = I('param.url_start','');
    		}
    		if(!empty(I('param.rule_match',''))){
    			$data['rule_match'] = I('param.rule_match','');
    		}
    		if(I('param.count_thread_max','') !=null){
    			$data['count_thread_max'] = I('param.count_thread_max','');
    		}
    		if(I('param.time_interval')!=null){
    			$data['time_interval'] = I('param.time_interval','');
    		}
    		if(!empty(I('param.schedule_time',''))){
    			$data['schedule_time'] = I('param.schedule_time','');
    		}
    		if(I('param.schedule_type','') != null){
    			$data['schedule_type'] = I('param.schedule_type','');
    		}
    		if(!empty(I('param.search_key',''))){
    			$data['search_key'] = I('param.search_key','');
    		}
    		if(!empty(I('param.search_id',''))){
    			$data['search_id'] = I('param.search_id','');
    		}
    		$taskTable->where($map)->save($data);
    		if($data ['schedule_type'] == 2){
    			$scheduleArr = I('param.schedule_data',array());
    			$scheduleTable = M('Schedule');
    			$scheduleTable->where(array('task_id'=>I('param.id')))->delete();
    			if($scheduleArr){
    				foreach ($scheduleArr as $schedule){
    					$scheduleData = array();
    					$scheduleData['task_id']= I('param.id');
    					$scheduleData['time']= $schedule;
    					$scheduleTable->add($scheduleData);
    				}
    			}
    		}
    		echo json_encode(array('result'=>'修改成功','code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    public function deleteTask(){
    	if(session('name')){
    		$taskTable = D('Task');
	    	$taskTable->delete(I('param.id',''));
	    	$scheduleTable = M('Schedule');
	    	$scheduleTable->where(array('task_id'=>I('param.id')))->delete();
    		echo json_encode(array('result'=>'删除成功','code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    public function getTaskById(){
    	if(session('name')){
    		$taskTable = D('Task');
    		$task = $taskTable->where(array('id'=>I('param.id')))->select();
    		echo json_encode(array('result'=>$task,'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }

}